﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using V5_Common;
using V5_BLL;
using V5_Model;
using System.Web.Security;

namespace V5_EShop._Template.Default
{
    public partial class login : BasicPage
    {
        private string returnUrl = "";
        protected void Page_Load(object sender, EventArgs e)
        {
            returnUrl = Input.SetString(Request["returnurl"]);
        }

        private void InitializeComponent()
        {
            this.btnLogin.Click += new EventHandler(btnLogin_Click);
        }

        void btnLogin_Click(object sender, EventArgs e)
        {
            DateTime time = DateTime.Now.AddHours(1);
            string userName = this.txtUserName.Text;
            string userpwd = this.txtUserPwd.Text;
            if (userName == "")
            {
                Js.AlertAndGoHistory("用户名称不能为空!", -1);
                return;
            }
            if (userpwd == "")
            {
                Js.AlertAndGoHistory("用户密码不能为空!", -1);
                return;
            }
            if (this.chkremember.Checked)
            {
                time = DateTime.Now.AddDays(1);
            }
            BLLUser bll = new BLLUser();
            ModelUser model = new ModelUser();
            model.UserName = userName;
            model.UserPassword = Input.MD5(userpwd, 16);
            model = bll.UserLogin(userName, Input.MD5(userpwd, 16));
            if (model != null)
            {
                //添加cookies 加密cookies
                HttpCookie cookie = new HttpCookie("LoginStatus");
                cookie.Values.Add("account", model.UserName);
                cookie.Values.Add("pass", model.UserPassword);
                cookie.Expires = time;
                Response.Cookies.Add(cookie);
                HttpCookie cookie1 = new HttpCookie("membername", model.UserName);
                cookie1.Expires = time;
                Response.Cookies.Add(cookie1);

                HttpCookie cookie2 = new HttpCookie("memberid", model.ID.ToString());
                cookie2.Expires = time;
                Response.Cookies.Add(cookie2);
                if (returnUrl == "")
                {
                    Response.Redirect("/member/default.aspx");
                }
                else
                {
                    Response.Redirect(returnUrl);
                }
            }
            else
            {
                Pr.RegClientAlertAndRedirect(this, "对不起!你的账号密码有误!或者您没有注册!", HTTPS.ComeUrl);
               // Response.Redirect(HTTPS.ComeUrl);
            }
        }

        protected override void OnInit(EventArgs e)
        {
            InitializeComponent();
            base.OnInit(e);
        }
    }
}
